package comparable;

import java.lang.reflect.Array;
import java.util.*;

public class TestComparable {

    public static void main(String[] args) {
        System.out.println("——————————————————————————————————————————————————————————");
        String s="sdf";
        System.out.println(1+'A');
        System.out.println(s.hashCode());
        //equals
        Student student=new Student();
        student.setAge(2);
        student.setName("xcy");
        System.out.println(student.hashCode());
        System.out.println(student);
        //比较==(可以近似看作hashcode)与equals
        Student student2=new Student();
        student2.setAge(2);
        student2.setName("xcy");
        System.out.println(student==student2);
        System.out.println(student.equals(student2));
        System.out.println("——————————————————————————————————————————————————————————");
//////////////////////////////////////////////////////////
        //String自带compareable
        String strA="qwer";
        String strB="ewrtgfdgs";
        String strC="fadsf";
        System.out.println(strB.compareTo(strC));
        //自定义comparable
        Student student3=new Student("xcy",20,12);
        Student student4=new Student("xcy",13,20);
        Student student5=new Student("xcy",19,20);
        System.out.println(student3.compareTo(student4));
        //普通数组
        Student[] students={student3,student4,student5};
        Arrays.sort(students);
        for(Student st:students)
        {
            System.out.println(st);
        }
        //collection数组
        List<Student>listStudent=new ArrayList<Student>();
        listStudent.add(student3);
        listStudent.add(student4);
        listStudent.add(student5);
        Collections.sort(listStudent, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int substraction= o1.getAge()-o2.getAge();
                if(substraction>0)
                    return -1;
                else
                    return 1;
                //return 0;
            }
        });
        System.out.println(listStudent);
        //set
        TreeSet treeSet=new TreeSet(new Comparator<Teacher>() {
            @Override
            public int compare(Teacher o1, Teacher o2) {
                int substraction=o1.getAge()-o2.getAge();
                if (substraction>0)
                    return 1;
                else
                    return -1;
            }
        });
        Teacher teacher1=new Teacher("xcy",12);
        Teacher teacher2=new Teacher("xcy",54);
        Teacher teacher3=new Teacher("xcy",42);
        treeSet.add(teacher1);
        treeSet.add(teacher2);
        treeSet.add(teacher3);
        System.out.println(treeSet);
        //Collections.sort(treeSet);//不可以对set进行排序，只能对list排序

        System.out.println("——————————————————————————————————————————————————————————");


    }
}
